package com.yzxb.messystem.util;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class ResponseResult<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    // 状态码
    private int code;
    
    // 消息
    private String message;
    
    // 数据
    private T data;
    
    // 时间戳
    private LocalDateTime timestamp;
    
    // 其他附加信息
    private Map<String, Object> extra = new HashMap<>();
    
    // 构造方法
    public ResponseResult() {
        this.timestamp = LocalDateTime.now();
    }
    
    public ResponseResult(int code, String message) {
        this.code = code;
        this.message = message;
        this.timestamp = LocalDateTime.now();
    }
    
    public ResponseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = LocalDateTime.now();
    }
    
    // 成功响应（无数据）
    public static <T> ResponseResult<T> success() {
        return new ResponseResult<>(200, "操作成功");
    }
    
    // 成功响应（带数据）
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(200, "操作成功", data);
    }
    
    // 成功响应（自定义消息和数据）
    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult<>(200, message, data);
    }
    
    // 失败响应（默认错误码）
    public static <T> ResponseResult<T> error() {
        return new ResponseResult<>(500, "操作失败");
    }
    
    // 失败响应（自定义错误码和消息）
    public static <T> ResponseResult<T> error(int code, String message) {
        return new ResponseResult<>(code, message);
    }
    
    // 失败响应（自定义错误码、消息和数据）
    public static <T> ResponseResult<T> error(int code, String message, T data) {
        return new ResponseResult<>(code, message, data);
    }
    
    // 添加附加信息
    public ResponseResult<T> addExtra(String key, Object value) {
        this.extra.put(key, value);
        return this;
    }
    
    // Getters and Setters
    public int getCode() {
        return code;
    }
    
    public void setCode(int code) {
        this.code = code;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public T getData() {
        return data;
    }
    
    public void setData(T data) {
        this.data = data;
    }
    
    public LocalDateTime getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
    }
    
    public Map<String, Object> getExtra() {
        return extra;
    }
    
    public void setExtra(Map<String, Object> extra) {
        this.extra = extra;
    }
    
    @Override
    public String toString() {
        return "ResponseResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", timestamp=" + timestamp +
                ", extra=" + extra +
                '}';
    }
}